<template >
  <div class="add_con_form tl" v-loading="loading">
    <el-dialog
      title="添加管理员"
      :modal-append-to-body="false"
      :close-on-click-modal="false"
      :visible.sync="visible"
      :show="show"
      :before-close="closeDialog"
      @close="$emit('update:show',false)"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="data" :rules="dataYZ" ref="data" class="mar_top" label-width="100px">
        <el-form-item label="角色" prop="role_id">
          <el-select
            v-model="data.role_id"
            placeholder="请选择"
            @change="handChange"
            style="width:190px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="账号" prop="username">
          <el-col :span="4">
            <el-input placeholder="请输入账号" v-model="data.username" style="width:190px" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-col :span="4">
            <el-input
              type="password"
              placeholder="请输入密码"
              v-model="data.password"
              style="width:190px"
              clearable
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="确认密码" prop="repassword">
          <el-col :span="4">
            <el-input
              type="password"
              placeholder="请输入密码"
              v-model="data.repassword"
              style="width:190px"
              clearable
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="电话" prop="tel">
          <el-col :span="4">
            <el-input placeholder="请输入手机号" v-model="data.tel" style="width:190px" clearable></el-input>
          </el-col>
        </el-form-item>

        <div class="mar_bottom_15">
          <span class="sizeColor size_13 sp_style"></span>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="addSave('data')"
          >确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>


<style lang="less" scoped></style>

<script>
export default {
  data() {
    // eslint-disable-next-line no-useless-escape
    var ex = /^[A-Za-z0-9\-\_]+$/;
    var checkUser = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      } else {
        //  setTimeout(() => {
        if (!this.$fnc.regexp(ex, value)) {
          callback(new Error("用户名非法"));
        } else {
          callback();
        }
        // }, 1000)
      }
    };

    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.data.repassword !== "") {
          this.$refs.data.validateField("repassword");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.data.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      options: [
        {
          value: "",
          label: ""
        }
      ],
      value: "",
      data: {
        username: "",
        password: "",
        tel: "",
        role_id: "3"
      },
      loading: false,
      dataYZ: {
        username: [{ validator: checkUser, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur", min: 6 }],
        repassword: [{ validator: validatePass2, trigger: "blur", min: 6 }],
        tel: [{ min: 11, max: 11, message: "手机格式错误", trigger: "blur" }]
      },
      visible: this.show
    };
  },
  props: {
    show: Boolean
  },
  methods: {
    closeDialog() {
      this.$refs["data"].resetFields();
      this.visible = false;
    },
    getRoleList() {
      var data = {};
      this.$api.getAdminuser.getRoleOption(data).then(res => {
        this.options = res.data.result;
      });
    },

    handChange(val) {
      this.data.role_id = val;
    },
    addSave(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var params = {};
          params["role_id"] = this.data.role_id;
          params["username"] = this.data.username;
          params["password"] = this.$getCode(this.data.password);
          params["tel"] = this.data.tel;

          this.$api.getAdminuser.mySave(params).then(res => {
            if (res.data.code == 200 && res.data.result != null) {
              this.$emit("parent", "这里刷新父页面");
              this.$fnc.alertSuccess({ text: "添加成功" }); //成功弹出框

              this.visible = false;
            } else {
              this.$fnc.alertError(res.data.result); //失败弹出框
            }
          });
        }
      });
    }
  },
  watch: {
    value(val) {
      console.log("val" + val);
    },
    show() {
      console.log(this.show);
      this.visible = this.show;
    }
  },
  mounted() {
    this.getRoleList();
  },
  created() {}
};
</script>
